/*=================================================
    File:	zcom_uiIMWrite.c

	Module:	ui
	Author:	wqn	
=================================================*/
#include <zcom_uiIMMain.h>
#include <IMManager.h>
#include <ui.h>
#include <spr.h>
#include <phonebook.h>

//========================================================================
// function declare
//========================================================================
//extern int IM_OutBox_AddMsg(char * number, char * msg);
extern int IM_DraftBox_AddMsg(char * msg);
extern bool callCtrlSendIM(char * number, char * msg);
//========================================================================
// State data
//========================================================================
struct 
{
	char	msg[132];
	//char	msg[MAX_IM_SIZE];
	char	number[CFG_PHONEBOOK_DIGIT_MAX];
	//@@ reserved
	//char	domain[CFG_NETWORK_SERVER_MAX];
	//unsigned  port;
    int WizardMenuDepth;
} zcom_uiIMWriteEntryS;

//========================================================================
//  Local Function Declare
//========================================================================
static void zcom_uiMessageInformationReset(void);
static void zcom_WizardMenuDepthReset(void *);
static void zcom_WizardMenuDepthIncrement(void *);
static void zcom_WizardMenuDepthDecrement(void *);
static void zcom_WizardMenuPopAll(void);
static void zcom_uiIMWriteMessageStart(void *);
static void zcom_uiIMWriteMessageDone(void *, const char *);
static void zcom_uiIMWriteOptionStart(void *);
static void zcom_uiIMWriteNumberStart(void *);
static void zcom_uiIMWriteNumberDone(void *, const char *);
static void zcom_uiIMWriteNumberSearchStart(void *);
static void zcom_uiIMWriteNumberSearchDone(void *, const char *);
static void zcom_uiIMWriteAskForSaving(void *);
static void zcom_uiIMWriteAskForSavingDone(void *);
static void zcom_uiIMWriteAskForSending(void);
static void zcom_uiIMWriteAskForSendingDone(void *);
static void zcom_uiMessageInformationReset(void);
//========================================================================
// Static Menu data
//========================================================================
static const UIMenuNode MenuIMWriteOptionNodes[] = {
    {
        .text        = {LANG_Menu_IM_Send},
        .selectCB    = zcom_uiIMWriteNumberStart,
        .selectCBarg = NULL
    },
    {
        .text        = {LANG_Calllog_Select_save},
        .selectCB    = zcom_uiIMWriteAskForSaving,
        .selectCBarg = NULL
    },
};
static const UIMenuListInit MenuIMWriteOption = {
	
    .title = { LANG_SoftKey_Options },
    .nNodes = sizeof(MenuIMWriteOptionNodes) / sizeof(MenuIMWriteOptionNodes[0]),
    .currNode = 0,
    .nDispLines = 0,
    .nodes = MenuIMWriteOptionNodes
};
static bool reply_flag = false;
//========================================================================
// Wizard Functions
//========================================================================
static void zcom_WizardMenuDepthReset(void *unused)
{
    zcom_uiIMWriteEntryS.WizardMenuDepth = 0;
}

static void zcom_WizardMenuDepthIncrement(void *unused)
{
    zcom_uiIMWriteEntryS.WizardMenuDepth++;
}

static void zcom_WizardMenuDepthDecrement(void *unused)
{
    zcom_uiIMWriteEntryS.WizardMenuDepth--;
}

static void zcom_WizardMenuPopAll()
{
    int WizardMenuCount = zcom_uiIMWriteEntryS.WizardMenuDepth;
    zcom_WizardMenuDepthReset(NULL);

    while (WizardMenuCount > 1)
    {
        uiMenuPop(0);
        WizardMenuCount--;
    }

    if (WizardMenuCount == 1)
    {
        uiMenuPop(1);
        WizardMenuCount--;
    }
}

//========================================================================
// Private Functions
//========================================================================
static void zcom_uiIMWriteMessageStart(void * arg)
{
    UIMenuPKeyTextEntryInit init = {};
    
    init.title.textIdx  = LANG_Menu_IM_WRITE;
    init.pkeyCB        = zcom_uiIMWriteMessageDone;
    sprcpy(init.str, sizeof(init.str), zcom_uiIMWriteEntryS.msg);
    //init.bufSize        = 90;//MAX_IM_SIZE;
    init.cursorPos      = strlen(init.str);
    init.cleanupCB      = zcom_WizardMenuDepthDecrement;

    //char number[CFG_PHONEBOOK_DIGIT_MAX+1] = {};
    //sprcpy(number, sizeof(number), (char)arg);
    char *number = (char *)arg;
    if ((number == NULL) || (number[0] == '\0'))
    {
        //init.nodeCBArgEx = arg;
	reply_flag = false;
        init.nodeCBArgEx = NULL;
        init.pkeyTextNilEntryIdx = LANG_SoftKey_Options;
        init.pkeyTextNotNilEntryIdx = LANG_SoftKey_Options;
    }
    else
    {
	reply_flag = true;
        sprcpy(zcom_uiIMWriteEntryS.number, 
		sizeof(zcom_uiIMWriteEntryS.number), 
		number);
        //init.nodeCBArgEx = number;
        init.nodeCBArgEx = arg;
        init.pkeyTextNilEntryIdx = LANG_Menu_IM_Send;
        init.pkeyTextNotNilEntryIdx = LANG_Menu_IM_Send;
    }
    //init.nodeCBArgEx = arg;//wqn.add TextExEntry
    //init.pkeyTextNilEntryIdx = LANG_SoftKey_Options;
    //init.pkeyTextNotNilEntryIdx = LANG_SoftKey_Options;
    
    uiMenuPKeyTextEntryInit(&init);
    zcom_WizardMenuDepthIncrement(NULL);
}

static void zcom_uiIMWriteMessageDone(void *data, const char *str)
{
    if ((str == NULL) || (str[0] == '\0'))
    {
		uiMenuDialogMesgTimed(
			LANG_Menu_IM_Warning,
			langGet(LANG_Menu_IM_Message_Is_Empty)
			);
		
        return;
    }
    	
    if (strcmp(str, zcom_uiIMWriteEntryS.msg) != 0)
    {
        sprcpy(zcom_uiIMWriteEntryS.msg,
               sizeof(zcom_uiIMWriteEntryS.msg),
               str
               );
    }

    //char number[CFG_PHONEBOOK_DIGIT_MAX+1] = {};
    //sprcpy(number, sizeof(number), (char)data);
    char *number = (char *)data;

    //if ((number == NULL) || (number[0] == '\0'))
    if ((zcom_uiIMWriteEntryS.number == NULL) 
		    || (zcom_uiIMWriteEntryS.number[0] == '\0'))
    {
        zcom_uiIMWriteOptionStart(NULL);
    }
    else
    {
		zcom_uiIMWriteAskForSending();
    }


}

static void zcom_uiIMWriteOptionStart(void *arg)
{
    uiMenuListInit (&MenuIMWriteOption);
    zcom_WizardMenuDepthIncrement(NULL);
}

static void zcom_uiIMWriteNumberStart(void *data)
{
    UIMenuPKeyTextEntryInit init = {};
    
    init.title.textIdx  = LANG_Menu_IM_Send_To;
    init.pkeyCB        = zcom_uiIMWriteNumberDone;
    //@init.str max size is 132
    sprcpy(init.str, sizeof(init.str), zcom_uiIMWriteEntryS.number);
    init.bufSize        = 90;//MAX_IM_SIZE;
    init.cursorPos      = strlen(init.str);
    init.cleanupCB      = zcom_WizardMenuDepthDecrement;
    //init.nodeCBArgEx = arg;//wqn.add TextExEntry
    init.pkeyTextNilEntryIdx = LANG_Menu_Call_Search_title;
    init.pkeyTextNotNilEntryIdx = LANG_Menu_IM_Send;
    
    uiMenuPKeyTextEntryInit(&init);
    zcom_WizardMenuDepthIncrement(NULL); 
}

static void zcom_uiIMWriteNumberDone(void *data, const char *str)
{
     
    if ((str == NULL) || (str[0] == '\0'))
    {
		if ( PhoneBook_GetCount() == 0 )
		{
			uiMenuDialogMesgTimed( LANG_PhoneBook_title,
				langGet(LANG_PhoneBook_empty));
			//return;
		}
		else
		{
			zcom_uiIMWriteNumberSearchStart(NULL);
		}	
		
    }
    else 
    {
        sprcpy(zcom_uiIMWriteEntryS.number,
			sizeof(zcom_uiIMWriteEntryS.number),
			str
			);
		zcom_uiIMWriteAskForSending();
    }
}
//
static void zcom_uiIMWriteNumberSearchStart(void *data)
{
    UIMenuText_List_PBook_V2_EntryInit  init;
    memset(&init, 0, sizeof(init));

    init.title.textIdx   = LANG_PhoneBook_search_name;
    //init.title.textIdx   = LANG_NULL;

    //init.prompt.textIdx  = LANG_PhoneBook_enter_search_name;
    init.doneCB          = zcom_uiIMWriteNumberSearchDone;
    init.cleanupCB      = zcom_WizardMenuDepthDecrement;
    init.pkeyTextEntryIdx = LANG_Menu_IM_Send;

    init.str[0]          = '\0';
    init.bufSize          = CFG_PHONEBOOK_NAME_SZ;
    init.cursorPos       = 0;
    init.shift           = 1;
    //init.nodeCBArgEx = data;

    uiMenuText_List_PBook_V2_EntryInit(&init);
    zcom_WizardMenuDepthIncrement(NULL);
}
static void zcom_uiIMWriteNumberSearchDone(void *data, const char *str)
{
    
    if ((str == NULL) || (str[0] == '\0'))
    {
		
		uiMenuDialogMesgTimed(
			LANG_Menu_IM_Warning,
			langGet(LANG_Menu_Phonebook_Number_Empty)
			);
		
		zcom_uiIMWriteNumberStart(NULL);
		return;
    }
    	
    if (strcmp(str, zcom_uiIMWriteEntryS.number) != 0)
    {
        sprcpy(zcom_uiIMWriteEntryS.number,
			sizeof(zcom_uiIMWriteEntryS.number),
			str
			);
		//zcom_uiIMWriteNumberStart((void *)str);
		zcom_uiIMWriteAskForSending();
		return;
    }
   
}

//save to draftbox
static void zcom_uiIMWriteAskForSaving(void *arg)
{
#if 0
    uiMenuDialogMesgYes(
		LANG_Calllog_Select_save,
		langGet(LANG_Menu_IM_Write_Save_Dialing_Confirm),
		zcom_uiIMWriteAskForSavingDone,
		0,
		NULL,
		NULL,
		NULL
		);
#else
        zcom_WizardMenuDepthDecrement(NULL);
	zcom_uiIMWriteAskForSavingDone(NULL);
#endif
}

static void zcom_uiIMWriteAskForSavingDone(void *arg)
{
    /*
    UIMenuText title;
    memset(&title, 0, sizeof(title));

    title.textIdx = LANG_Menu_IM_Write_Option_Save;
    title.textCB = NULL;	
    title.textCBarg = NULL; 
    */
    uiMenuPop(0);
    zcom_WizardMenuPopAll();
    //uiMenuPop(1);
    //zcom_WizardMenuDepthDecrement(NULL);
    

    if (IM_DraftBox_AddMsg(zcom_uiIMWriteEntryS.msg)<0)
    {
		//uiMenuDialogMesgOk(title, 
		//		langGet(LANG_Menu_IM_Write_Save_Failed));
		uiMenuDialogMesgTimed(
			LANG_Calllog_Select_save,
			langGet(LANG_Menu_IM_Save_Fail)
			);
    }
    else
    {
		//uiMenuDialogMesgOk(title, 
		//		langGet(LANG_Menu_IM_Write_Save_Finish));
		uiMenuDialogMesgTimed(
			LANG_Calllog_Select_save,
			langGet(LANG_Menu_IM_Save_Ok)
			);
    }
}

static void zcom_uiIMWriteAskForSending()
{
#if 0
    uiMenuDialogMesgYes(
		LANG_Menu_IM_Write_Option_Send,
		langGet(LANG_Menu_IM_Write_Send_Dialing_Confirm),
		zcom_uiIMWriteAskForSendingDone,
		0,
		NULL,
		NULL,
		NULL);
#else
	zcom_uiIMWriteAskForSendingDone(NULL);
#endif

}

static void zcom_uiIMWriteAskForSendingDone(void *arg)
{
    /*
    UIMenuText title;
    memset(&title, 0, sizeof(title));

    title.textIdx = LANG_Menu_IM_Write_Option_Send;
    title.textCB = NULL;	
    title.textCBarg = NULL; 
    */
    uiMenuPop(0);
    zcom_WizardMenuPopAll();
    
    if (reply_flag != false)
    {
    		uiMenuPop(0);
		uiMenuPop(0);
    }
    reply_flag = false;

    //callCtrlSendIM( zcom_uiIMWriteEntryS.number, zcom_uiIMWriteEntryS.msg);
    //IM_OutBox_AddMsg( zcom_uiIMWriteEntryS.number, zcom_uiIMWriteEntryS.msg);
    int err = IMManager_SendOutMsg(zcom_uiIMWriteEntryS.number, 
		    zcom_uiIMWriteEntryS.msg);
    if(err<0)
	{
		//uiMenuDialogMesgOk(title, 
		//		langGet(LANG_Menu_IM_Send_Fail));
		uiMenuDialogMesgTimed(
			LANG_Menu_IM_Send,
			langGet(LANG_Menu_IM_Send_Fail)
			);
    }
    else
    {
		//IM_OutBox_AddMsg( zcom_uiIMWriteEntryS.number, 
		//		    zcom_uiIMWriteEntryS.msg);
		//uiMenuDialogMesgOk(title, 
		//		langGet(LANG_Menu_IM_Send_Ok));
		uiMenuDialogMesgTimed(
			LANG_Menu_IM_Send,
			langGet(LANG_Menu_IM_Send_Ok)
			);
    }
    
}

static void zcom_uiMessageInformationReset()
{
    zcom_uiIMWriteEntryS.number[0] = '\0';
    zcom_uiIMWriteEntryS.msg[0] = '\0';
}

//========================================================================
// Public Functions
//========================================================================
void zcom_uiIMWriteStart(void * arg)
{
    zcom_uiMessageInformationReset();
    zcom_WizardMenuDepthReset(NULL);

    zcom_uiIMWriteMessageStart(arg);
}
